package sgi.client;

import sgi.details_new.ImageDetails;
import sgi.message.MessageSearch;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;
/**
 * Interactive Map controller class.
 * Retrieves location list from server and creates the UI.
 * */
public class SGIInteractiveMapController extends SGIController {
	
	private static SGIInteractiveMapController interactiveMap = null;
	private static SGIInteractiveMapUI interactiveMapUI;
	//private static Iterator<ImageDetails> iterator;
	private static List<ImageDetails> list;
	private static ImageDetails [] imgArray;
	private ImageDetails helpfulImage;
	
	/**
	 * A static fuction which calls on the constructor of this class
	 * if an instance doesn't already exists.
	 * returns itself as an object.
	 * */
	public static SGIInteractiveMapController create() throws IOException{
		if (SGIInteractiveMapController.interactiveMap == null) {
			interactiveMap = new SGIInteractiveMapController();
	//		interactiveMapUI.setController(interactiveMap);
		}
	//	interactiveMapUI.setVisible(true);
		return interactiveMap;
	}
	
	/**
	 * The actual constructor. Calls on the Search method of the object
	 * */
	public SGIInteractiveMapController() {
		this.search();
		//interactiveMapUI = new SGIInteractiveMapUI();
	}
	
	/**
	 * sends a message requesting a search of all images
	 * in every location, between extreme ranges
	 * so that it retrieves details of all the images.
	 * Sets the message type to 2 so that the server knows
	 * to send it to this object's HandleMessage
	 * */
	private void search()
	{
		MessageSearch msg = new MessageSearch("1000-01-01","9999-12-31");
		msg.setSearchMessageType(2);
		sendSearch(msg);
	}
	
	/**
	 * Sends the search message to the server
	 * */
	private void sendSearch(MessageSearch msg)
	{
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	
	
	/**
	 * Terminates the instance
	 * */
	public static void terminate()
	{
		SGIInteractiveMapController.interactiveMap = null;
		SGIInteractiveMapController.interactiveMapUI.setController(null);
		SGIInteractiveMapController.interactiveMapUI.setVisible(false);
		SGIInteractiveMapController.interactiveMapUI = null;
		SGIUserAccountMenuController.create(null);
	}
	
	
	/**
	 * Receives list of image details from server.
	 * Creates the UI and ultimately the interactive map.
	 * */
	@Override
	void handleMessage(Object msg) 
	{
		if(((MessageSearch)msg).getImages() == null){
			System.out.println("No locations in database.");
			SGIInteractiveMapController.terminate();
			return;
		}
		interactiveMapUI = new SGIInteractiveMapUI(((MessageSearch)msg).getImages());
		interactiveMapUI.setController(interactiveMap);	
	}
	
	public static SGIInteractiveMapUI getUI()
	{
		return interactiveMapUI;
	}

	public List<ImageDetails> getLocationList() {
		return SGIInteractiveMapController.list;
		
	}
}
